// SCENARIO SCRIPT

// This is the special script for your entire scenario. It contains
// special encounters and code accessible from anywhere in the scenario. it also
// contains the code that initializes important special things in the
// scenario (like shops and names and descriptions of special items).

// You can create your own states, but you should give all of them numbers greater than
// or equal to 10.

//
// global flags: 50, 0  = counter for energy drainer
//               50, 1  = solved mystery of vanished family
//               50, 2  = quitting, without solving mystery...
//               50, 3  = have read initial journal
//               50, 4  = have read second journal
//               50, 5  = have read third journal
//               50, 6  = have read final journal
//               50, 7  = door number to final journal
//               50, 8  = found initial journal
//               50, 9  = found second journal
//               50, 10 = found third journal
//               50, 11 = found fourth journal
//               100, 0 = vortex destruction counter
//               101, 0 = elec vortex dead
//               101, 1 = water vortex dead
//               101, 2 = fire vortex dead
//               101, 3 = air vortex dead
//

beginscenarioscript;

variables;

short choice, i, e, c, ct;
string msgn;

body;

// This is the state that is called every time the scenario is loaded,
// even when a save file in the scenario is loaded. Some things that should go here:
//    Names and descriptions of special items.
//    Names and effects of custom special abilities.
beginstate LOAD_SCEN_STATE;
	set_flag(50, 0, 0);
break;

// This is the state that is called only once at the very beginning of 
// the scenario. Some things that should go here:
//    The stuff in shops.
//    Creating horses and boats.
beginstate START_SCEN_STATE;
	set_flag(50, 7, get_ran(1, 1, 10));
break;

// This state is called every tick wherever the party is in the scenario.
// You can use the set_state
beginstate START_STATE;
	// zero spell energy every 2 steps
	if (get_flag(50, 0) >= 2) {
		set_flag(50, 0, 0);
		set_state_continue(10);
	}
	else {
		inc_flag(50, 0, 1);
	}
break;

// Place your own states below. Give each a number at least 10.
beginstate 10;
	// print_str_color("Scen St 10.", 2);
	i = 0;
	while (i < 4) {
		if (char_ok(i) == TRUE) {
			e = get_energy(i);
			if (e > 0) {
				e = 0 - e;
				change_char_energy(i, e);
			}
		}
		i = i + 1;
	}
	// if not here, start state will never get called again, only this state each move
	set_state(START_STATE);
break;

beginstate 11;
	// lightning rod...
	c = who_is_custom_item_target();
	// print_big_str("Target = ", c, "");
	ct = creature_type(c);
	// print_big_str("Type = ", ct, "");
	if (ct == 234) {
		put_boom_on_char(c, 0, FALSE);
		run_animation_sound(164);
		reset_dialog();
		add_dialog_str(0, "You use the lightning rod against the vortex.", 0);
		add_dialog_str(1, "With a loud snap, all of the vortex's energy is grounded through the lightning rod.", 0);
		add_dialog_str(2, "The vortex crackles and pops a few times, then fades away.", 0);
		add_dialog_str(3, "The house seems less ominous now.", 0);
		add_dialog_choice(0, "Ok");
		choice = run_dialog(1);
		kill_char(c, 2, FALSE);
	}
	else {
		print_str_color("The item doesn't work against that creature.", 2);
	}
break;

beginstate 12;
	// bucket of dirt...
	c = who_is_custom_item_target();
	ct = creature_type(c);
	if (ct == 236) {
		put_boom_on_char(c, 4, FALSE);
		run_animation_sound(169);
		reset_dialog();
		add_dialog_str(0, "You throw your bucket of dirt on the vortex.", 0);
		add_dialog_str(1, "The dirt smothers the vortex's flames.", 0);
		add_dialog_str(2, "The vortex gets smoky, then dies away.", 0);
		add_dialog_str(3, "The house seems to get a little lighter.", 0);
		add_dialog_choice(0, "Ok");
		choice = run_dialog(1);
		kill_char(c, 2, FALSE);
	}
	else {
		print_str_color("The item doesn't work against that creature.", 2);
	}
break;

beginstate 13;
	// large towel...
	c = who_is_custom_item_target();
	ct = creature_type(c);
	if (ct == 235) {
		put_boom_on_char(c, 6, FALSE);
		run_animation_sound(108);
		reset_dialog();
		add_dialog_str(0, "You use your large towel on the vortex.", 0);
		add_dialog_str(1, "The towel starts drying up the vortex, sopping up the water, protecting you from it's affects.", 0);
		add_dialog_str(2, "Eventually, the water vortex is dried up and gone.", 0);
		add_dialog_str(3, "The house is definitely not as dark as before.", 0);
		add_dialog_choice(0, "Ok");
		choice = run_dialog(1);
		kill_char(c, 2, FALSE);
	}
	else {
		print_str_color("The item doesn't work against that creature.", 2);
	}
break;

beginstate 14;
	// fan...
	c = who_is_custom_item_target();
	ct = creature_type(c);
	if (ct == 237) {
		put_boom_on_char(c, 2, FALSE);
		run_animation_sound(163);
		reset_dialog();
		add_dialog_str(0, "You start fanning the vortex.", 0);
		add_dialog_str(1, "The fans blades start dissipating the vortex, breaking it up.", 0);
		add_dialog_str(2, "Soon, all that is left is a few wisps of air, that gradually fade away.", 0);
		add_dialog_str(3, "The house practically beams.", 0);
		add_dialog_choice(0, "Ok");
		choice = run_dialog(1);
		kill_char(c, 2, FALSE);
	}
	else {
		print_str_color("The item doesn't work against that creature.", 2);
	}
break;

// initial (first) journal text
beginstate 15;
	reset_dialog();
	add_dialog_str(0, "The journal appears to have been written by Dan, the man of the house.  It reads:", 0);
	add_dialog_str(1, "Day 1: Ah, now that I have succeeded in creating the healing statue, I turn my sights on creating something to aid in cleaning this manor.", 0);
	add_dialog_str(2, "Day 2: After some research, I have decided to create some vortexes, one for each floor.  Hopefully I'll be able to control them.", 0);
	add_dialog_str(3, "Day 5: Studied vortexes and believe that I have a way to get them created.  I may need to think twice about using body parts.", 0);
	add_dialog_str(4, "Day 7: No way around it, I'm going have to dig in the graveyard in order to create the vortexes.  Just hope Liz doesn't see...", 0);
	add_dialog_str(5, "Day 11: Have had some success.  Created my first vortex, but it faded away before I could stabilize it.", 0);
	add_dialog_choice(0, "Next Page");
	add_dialog_choice(1, "Quit");
	choice = run_dialog(1);
	if (choice == 1) {
		set_state_continue(16);
	}
break;

beginstate 16;
	set_flag(50, 3, 1); // read through first journal, setup to find next one.
	reset_dialog();
	add_dialog_str(0, "Day 16: Success at last.  Manage to get a vortex created and stabilized.  Was just barely able to get it under control.", 0);
	add_dialog_str(1, "Day 18: Put the first in the attic to start cleaning while creating a second.  The first one seems to be working fine, though haven't told Liz about it yet.", 0);
	add_dialog_str(2, "Day 22: Have created two more.  It seems to get harder and harder to control them the more of them there are.  Don't know why.", 0);
	add_dialog_str(3, "Day 25: Finished the forth and have lost control of the vortexes.  Managing to stay away from them for now, don't know for how much longer though.", 0);
	add_dialog_str(4, "Day 26: Owww.  My wife and kids have been sucked into the vortexes.  Must find a way to save them...", 0);
	add_dialog_str(5, "At the bottom of the page is scrawled 'hidden the second in the library.'", 0);
	add_dialog_choice(0, "Quit");
	add_dialog_choice(1, "Prev Page");
	choice = run_dialog(1);
	if (choice == 2) {
		set_state_continue(15);
	}
break;

// second journal text
beginstate 17;
	reset_dialog();
	add_dialog_str(0, "Day 27: The vortexes don't stop.  Each still cleans like nothing has happened.  My family...", 0);
	add_dialog_str(1, "Day 28: Started studying the vortexes.  Have to move around all the time to avoid them.", 0);
	add_dialog_str(2, "Day 31: Managed to get them all upstairs.  Can at least move around the first floor and the basement without having to avoid the vortexes.", 0);
	add_dialog_str(3, "Day 32: Setup my studies in the laboratory.  Will sleep there until I find a way to reverse what I've done.", 0);
	add_dialog_str(4, "Day 36: With a little more research and testing, I believe if the vortexes can be destabilized and made to disappear, my family will be returned.", 0);
	add_dialog_str(5, "Just need to figure out how to make that happen.", 2);
	add_dialog_choice(0, "Next Page");
	add_dialog_choice(1, "Quit");
	choice = run_dialog(1);
	if (choice == 1) {
		set_state_continue(18);
	}
break;

beginstate 18;
	set_flag(50, 4, 1); // read through second journal, setup to find next one.
	reset_dialog();
	add_dialog_str(0, "Day 38: I've made some progress in determining how to destroy the vortexes I've created.  Shouldn't be long now.", 0);
	add_dialog_str(1, "Day 41: Was able to freeze and hide all of the vortexes except the one in the attic.  Once I figure out the destruction method, I'll bring them back.", 0);
	add_dialog_str(2, "The one in the attic almost got me.  I was studying it from the door and it somehow got behind me.  Need to watch out.", 0);
	add_dialog_str(3, "Day 43: I think I have it.  Each one will need something different to destabilize and destroy it.  Have what I need for one of them, just need to collect the rest.", 0);
	add_dialog_str(4, "Day 44: Going up to freeze/hide the one in the attic so I can take on one at a time.  Still need to collect more stuff.  Just in case, hiding my journals.", 0);
	add_dialog_str(5, "Scrawled in the bottom of the page is 'Took the journal with the destabilization instructions to the basement, laboratory doesn't seem safe anymore.'", 0);
	add_dialog_choice(0, "Quit");
	add_dialog_choice(1, "Prev Page");
	choice = run_dialog(1);
	if (choice == 2) {
		set_state_continue(17);
	}
break;

// third journal text
beginstate 19;
	reset_dialog();
	add_dialog_str(0, "The journal has a set of instructions in it that Dan has collected to destroy the vortexes.", 0);
	add_dialog_str(1, "Created 4 vortexes.  Took the easy way and used the dead which added an evil element to their makeup.", 0);
	add_dialog_str(2, "In order to destroy them, need to use the following items.", 0);
	add_dialog_str(3, "The electrical like vortex just needs to be grounded.  I believe the manor's lightning rod will work.  Have it in the laboratory currently.", 0);
	add_dialog_str(4, "The fire like vortex can be defeated by throwing a bucket of dirt on it.  Need to collect this still.", 0);
	add_dialog_str(5, "The water like vortex should be able to be dried up.  I believe a large towel should do the trick.  I know we have some around here someplace.", 0);
	add_dialog_choice(0, "Next Page");
	add_dialog_choice(1, "Quit");
	choice = run_dialog(1);
	if (choice == 1) {
		set_state_continue(20);
	}
break;

beginstate 20;
	set_flag(50, 5, 1); // read through third journal, setup to find next one.
	reset_dialog();
	add_dialog_str(0, "The airy vortex will be tough.  But I believe that by using a large fan, it can be dissipated.", 0);
	add_dialog_str(1, "The vent fan in the basement should work.  Penned in on the side is 'need to find a screwdriver for this.'", 0);
	add_dialog_str(2, "Just need to make one more visit to make sure of my instructions, but I believe these to be the way.", 0);
	add_dialog_str(3, "Will put the unfreeze/unhide instructions in my trap room behind the hidden door before the exercise yard.", 0);
	add_dialog_str(4, "Currently the untrapped door is number...", 0);
	add_dialog_str(5, "The page appears to have been torn out of the journal, possibly by a nail in the bookshelf it was originally on.", 0);
	add_dialog_choice(0, "Quit");
	add_dialog_choice(1, "Prev Page");
	choice = run_dialog(1);
	if (choice == 2) {
		set_state_continue(19);
	}
break;

// fourth journal text
beginstate 21;
	reset_dialog();
	add_dialog_str(0, "The journal contains a set of instructions for reactivating the vortexes so they can be destroyed.", 0);
	add_dialog_str(1, "VORTEX UNFREEZE INSTRUCTIONS", 5);
	add_dialog_str(2, "The following instructions are dangerous.  If someone is reading this, I failed.", 0);
	add_dialog_str(3, "I created four vortexes in ignorance, hoping to make life easier, but instead made life harder.", 0);
	add_dialog_str(4, "I was able to get the vortexes trapped in the upper floors of this manor and freeze them until I was ready to attempt to destroy them.", 0);
	add_dialog_str(5, "Do not turn the page unless you are ready to destroy the vortexes.  'Good luck and God speed' is penciled in the bottom margin.", 0);
	add_dialog_choice(0, "Next Page");
	add_dialog_choice(1, "Quit");
	choice = run_dialog(1);
	if (choice == 1) {
		set_state_continue(22);
	}
break;

beginstate 22;
	set_flag(50, 6, 1); // read through fourth journal, setup for end game.
	reset_dialog();
	add_dialog_str(0, "I managed to put them in rooms upstairs and freeze them with high frequency sound waves.", 0);
	add_dialog_str(1, "I've create a flute, that when used, will pierce the high frequency waves, releasing the vortex.", 0);
	add_dialog_str(2, "The flute is on a shelf in the laboratory.  I was not able to get it to a safer place, yet.  Hopefully, it is still there.", 0);
	add_dialog_str(3, "Use the flute outside of the doors upstairs.  This will release the vortex so it can be returned back to where it came from.", 0);
	add_dialog_str(4, "Do not use the flute if you are not ready to handle the vortexes.", 0);
	add_dialog_str(5, "Yet again, 'Good luck.' is the last thing scrawled onto the page.", 0);
	add_dialog_choice(0, "Quit");
	add_dialog_choice(1, "Prev Page");
	choice = run_dialog(1);
	if (choice == 2) {
		set_state_continue(21);
	}
break;

// torn out journal page (from third)
beginstate 23;
	clear_buffer();
	append_string("The torn page has a single number on it: ");
	append_number(get_flag(50, 7));
	get_buffer_text(msgn);
	message_dialog("This appears to be a page torn from a journal.", msgn);
break;
